<?php

/**
 * @author quang
 * @copyright 2011
 */
require_once ('general.php');

class UserDal
{

    function save($obj)
    {
        if ($obj instanceof User) {
            #insert into database
            if (tep_db_checkDb()) {
                #if connect ok, insert into db
                $selectQuery = "select id from " . TBL_CUSTOMER . " where uid='" . $obj->getUid() .
                    "'";
                $result = tep_db_query($selectQuery);
                if (!tep_db_fetch_array($result)) {
                    $query = "insert into " . TBL_CUSTOMER . " (uid, name, email) values('" . $obj->
                        getUid() . "', '" . $obj->getName() . "', '" . $obj->getEmail() . "')";
                    tep_db_query($query);
                    $obj->setId(tep_db_insert_id());
                } else {
                    $row = tep_db_fetch_array($result);
                    $obj->setId($row['id']);
                }
                return $obj;
            } else {
                # if not
                die('Cannot connect to database');
            }
        } else {
            die('Object is not User object so can not save to database');
        }
    }
    function saveScore($obj)
    {
        if ($obj instanceof User) {
            #insert into database
            if (tep_db_checkDb()) {
                #if connect ok, save score and answer
                $query = "insert into " . TBL_CUSTOMER_ANSWER . " (customer_id, answer, score, date_play) values('" . $obj->
                    getId() . "', '" . $obj->getAnswer() . "', '" . $obj->getScore() . "', NOW() )";
                tep_db_query($query);
                $obj->setId(tep_db_insert_id());                
                return $obj;
            } else {
                # if not
                die('Cannot connect to database');
            }
        } else {
            die('Object is not User object so can not save to database');
        }
    }
}

class SpeciesDal{
    function selectAll(){
        if (tep_db_checkDb()) {
            #if connect ok, insert into db
            $selectQuery = "select * from " . TBL_SPECIES;
            $arrSpecies = array();
            $result = tep_db_query($selectQuery);
            while($item = tep_db_fetch_array($result)){
                   $specy = new Species($item["content_en"], $item["content_fr"], $item["Img"]);
                   $specy->setId(intval($item["Id"]));
                   array_push($arrSpecies, $specy);
            }
            return $arrSpecies;
        } else {
            # if not
            die('Cannot connect to database');
        }
    }
}

class QuizzDal{
    function selectAll(){
        if (tep_db_checkDb()) {
            #if connect ok, insert into db
            $selectQuery = "select * from " . TBL_QUIZZ;
            $arrQuizzes = array();
            $result = tep_db_query($selectQuery);
            while($item = tep_db_fetch_array($result)){
                   $quiz = new Quizz(intval($item["Id"]),$item["quiz_en"], $item["quiz_fr"], $item["SpecyId"]);                   
                   array_push($arrQuizzes, $quiz);
            }
           # print_r($arrQuizzes);
            return $arrQuizzes;
        } else {
            # if not
            die('Cannot connect to database');
        }
    }
}

?>